package com.css.zfzx.data.primary.fxqhHdc100PG.repository.impl;

import com.css.zfzx.data.primary.fxqhHdc100PG.repository.FxqhHdc100PGNativeRepository;
import com.css.zfzx.data.primary.fxqhHdc100PG.repository.entity.FxqhHdc100PGEntity;
import com.css.zfzx.data.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * @author zyb
 * @date 2020-11-30
 */
@Repository
@PropertySource("classpath:platform-config.yml")
public class FxqhHdc100PGNativeRepositoryImpl implements FxqhHdc100PGNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(FxqhHdc100PGNativeRepositoryImpl.class);


    @Override
    public List<FxqhHdc100PGEntity> findYhDisastersCreate(LocalDateTime createTime) {
        StringBuilder sql = new StringBuilder("select * from fxqh_hdc100 ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (createTime!=null){
            whereSql.append(" and create_time >:createTime");
        }else {
            whereSql.append(" and create_time is not null");
        }
        whereSql.append(" and is_valid = 1");
        whereSql.append(" and update_time is null");

        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxqhHdc100PGEntity.class);
        if (createTime!=null){
            query.setParameter("createTime",createTime);
        }
        List<FxqhHdc100PGEntity> list = query.getResultList();
        return list;
    }

    @Override
    public List<FxqhHdc100PGEntity> findYhDisastersUpdate(LocalDateTime updateTime) {
        StringBuilder sql = new StringBuilder("select * from fxqh_hdc100 ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (updateTime!=null){
            whereSql.append(" and update_time >:updateTime");
        }else {
            whereSql.append(" and update_time is not null");
            whereSql.append(" and is_valid=1");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxqhHdc100PGEntity.class);
        if (updateTime!=null) {
            query.setParameter("updateTime", updateTime);
        }
        List<FxqhHdc100PGEntity> list = query.getResultList();
        return list;
    }
}
